/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MinhNH;

import Entity.Account;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author dukynguyen
 */
@Stateless
public class MinhNHSessionBean implements MinhNHSessionBeanLocal {
    @PersistenceContext(unitName = "Group05_NationalLevelPaperPresentation-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public boolean RegisterAccount(String username, String password, String fullname, String email, String gender, String birthday, String avatar, String address, String phone, String school) {
        boolean check = false;
        try {
            Account objA = new Account();
            objA.setUsername(username);
            objA.setPassword(password);
            objA.setAddress(address);
            objA.setFullname(fullname);
            objA.setAvatar(avatar);
            objA.setBirthday(birthday);
            objA.setGender(gender);
            objA.setEmail(email);
            objA.setSchool(school);
            objA.setPhone(phone);
            objA.setRole("User");
            objA.setDateJoin(new Date());
            objA.setIDelete(false);

            persist(objA);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean UpdateProfile(String username, String password, String fullname, String avatar, String address, String phone, String school) {
        boolean check = false;
        try {
            Account objA = em.find(Account.class, username);
            objA.setPassword(password);
            objA.setFullname(fullname);
            if(!avatar.trim().isEmpty())
            {
                objA.setAvatar(avatar);
            }
            objA.setAddress(address);
            objA.setPhone(phone);
            objA.setSchool(school);

            persist(objA);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean LockAccount(String username) {
        boolean check = false;
        try {
            Account objA = em.find(Account.class, username);
            objA.setIDelete(true);

            persist(objA);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean UnlockAccount(String username) {
        boolean check = false;
        try {
            Account objA = em.find(Account.class, username);
            objA.setIDelete(false);

            persist(objA);
            check = true;
        } catch (Exception e) {
        }
        return check;
    }

    public Entity.Account[] getAccount() {
        try {
            Query query = em.createNamedQuery("Account.findAll");
            Collection accList = query.getResultList();
            Account[] objA = new Account[accList.size()];
            accList.toArray(objA);
            return objA;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Account findAccountByPrimarykey(String username) {
        try {
            Account objA = em.find(Account.class, username);
            return objA;
        } catch (Exception e) {
        }
        return null;
    }

    public Entity.Account[] findEmailOfAccount(String email) {
        try {
            Query query = em.createQuery("SELECT a FROM Account a WHERE a.email = :email and a.iDelete = :iDelete");
            query.setParameter("email", email);
            query.setParameter("iDelete", false);
            List list = query.getResultList();
            Account[] result = new Account[list.size()];
            list.toArray(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean changePass(String user, String pass) {
        boolean check = false;
        try {
            Account objA = em.find(Account.class, user);
            objA.setPassword(pass);

            persist(objA);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
}
